<% content_for :css do %>
  <%= stylesheet_link_tag 'applications_index','users_index' %>
<% end %>
<% content_for :title do %>所有的员工<% end %>
<div id='users' class="grid_19 users">
  <div class="grid_19 head alpha omega">
    <div class="title">
      <b class='group'><%= @group.blank? ? '所有' : "#{@group.name}组" %></b><%= params[:key_words].blank? ? '' : "中含关键字'#{params[:key_words]}'的" %>『<%= t params[:status] %>』的员工<small>(共<%= @users.size %>名)</small>
    </div>
  </div>
  <div class="grid_14 alpha omega">
    <div class="grid_14 omega all_users ">       
      <% if flash[:success].present? %>
      <div id='success' class="grid_14">
        <div><%= flash[:success] %></div>
      </div>         
      <% end %>
      <div class="grid_14 alpha top">
        <div class="right">
        </div>
      </div>
      <% @users.each do |user| %>
      <div id='user_<%= user.id %>' class='grid_14 alpha omega user <%= cycle '','h' %> <%= user.status %> <%= user.groups.collect {|group| "belongs_to_group_#{group.id}"}.join(' ') %>'>
        <div class='grid_2 alpha logo'>          
          <%= image_tag "users/#{user.sex}.png", :size => '54x54' %>
          <% if user.is_admin? %>
          <div class='admin'>
            <%= link_to_function '管理员','',:title => '管理员' %>
          </div>
          <% else %>
          <div class='status <%= user.status %>'>
            <%= link_to_function t(user.status),'',:title => t(user.status) %>
          </div>
          <% end %>
        </div>
        <div class='grid_5 detail'>
          <div class='name'>
            <div>
              <%= user.full_name %>
            </div>
            <% if current_user.is_admin? %>
            <div>
              <%= link_to_function "移动","", :class => 'l_crud l_move', :title => '上下移动调整位置', :id => "move_user_#{user.id}"  %>
            </div>            
            <script type="text/javascript">
              $('#move_user_<%= user.id %>').mousedown(function(){
                $('#user_<%= user.id %>').addClass('dropping');
                $('.all_users').addClass('sortable');
                $( ".all_users" ).sortable({ 
                    items: '.user',
                    disabled:false,
                    update: function(event, ui) { 
                      $.post('<%= sort_users_url %>', $(this).sortable('serialize',{async: true}));
                      // console_debug( $(this).sortable('serialize'))
                    }
                });
              });
              $('#move_user_<%= user.id %>').mouseup(function(){
                $('#user_<%= user.id %>').removeClass('dropping');
                $('.all_users').removeClass('sortable');
                $( ".all_users" ).sortable({disabled:true});
              });
            </script>
            <% end %>
          </div>
          <div class='clear'></div>
          <div class='mobile'>
            <%= user.mobile %>
          </div>
          <div class='email'>
            <%= link_to user.email,"mailto:#{user.email}" %>
          </div>
          <div class='address'>
            <%= user.address %>
          </div>
        </div>
        <% if current_user.is_admin? %>
        <div class='grid_2 omega tools'>
          <%= link_to "编辑资料",edit_user_url(user),  :method => :get,:title => '编辑员工基本资料', :class => 'tool' %>
          <%= link_to "重设密码",reset_password_user_url(user, :group_id => params[:group_id]), :method => :put, :title => '重设密码' , :class => 'tool' %>
          <%  if user.status.active? %>
          <%= link_to "锁定账户",lock_user_url(user, :group_id => params[:group_id]), :method => :put, :title => '锁定该员工账户，令其无法登录，但系统各项功能的人员列表仍然可见', :class => 'tool' %>
          <%= link_to "停用账户",disable_user_url(user, :group_id => params[:group_id]), :method => :put, :title => '停用该员工账户，令其无法登录，并且系统各项功能的人员列表也将其屏蔽', :class => 'tool' %>
          <% else %>
          <%= link_to "激活账户",active_user_url(user, :group_id => params[:group_id]), :method => :put, :title => '激活该员工账户，允许其登录系统', :class => 'tool' %>
          <% end %>
        </div>
        <% end %>
        <div class='grid_10 push_2 groups'>
        <b>所在组：</b>
        <% user.groups.each do |group| %>
          <%= link_to group.name,users_url(:group_id => group.id), :title => "点击看『#{group.name}』的员工" %>
        <% end %>
        </div>
      </div>
      <% end %>
    </div>
  </div>
  <div id='right' class='grid_5 omega'>
    <div id="quick_search" class="block">
      <div class="title">根据员工姓名,手机号,email查询</div>
        <div class="alpha ">
          <%= form_for :users, 
                :url => users_url,
                :html => {
                  :method => :get,
                  :id => :users_form
                } do  %>
            <%= text_field_tag :key_words, '', :id => 'key_words' %>
            <%= hidden_field_tag :group_id, params[:group_id] %>
            <%= hidden_field_tag :status, params[:status] %>
          <% end %>          
        </div> 
        <div>
          <%= link_to_function "根据关键字查找员工", "$('#users_form').submit()", :class => :search_button, :title => '查找员工' %>
        </div>
        <div>
          <%= link_to "增加一名新员工", new_user_url, :class => :new_button, :title => '增加一名新员工' %>
        </div>
      </div>
    <div class="block">
      <div class="title">
        根据状态查询
      </div>
      <% ['active','locked','disabled'].each do |status| %>
      <div id="user_status_<%= status %>" class="item <%= status %> <%= params[:status] == status ? '': 'unchecked' %>" >
        <%= radio_button_tag 'user_status', status, params[:status] == status , :class => 'user_status_cb ', :id => "cb_user_status_#{status}" %> 
        <%= label_tag "cb_user_status_#{status}", t(status) , :class => 'user_status_label' %>
        <b class='count'><%= current_enterprise.users.where(:status => status).size %></b>
        <small>......</small>
      </div>
      <div class="clear"></div>
      <% end %>
      <script type="text/javascript">
        $('.user_status_cb').click(function() {
          $('.user_status_cb:checked').parent().removeClass('unchecked');
          $('.user_status_cb:not(:checked)').parent().addClass('unchecked');
          // console_debug($('.user_status_cb:not(:checked)').length)
          $('#users_form #status').val($('.user_status_cb:checked').val());
          // console_debug($('#users_form #status').length)
          $('#users_form').submit();
        })
      </script>
    </div>
    <div class='block'>
      <div class="title">
        根据分组查询
      </div>
      <div id="all_group" class="item ">
        <%= radio_button_tag 'group_id', '%', params[:group_id] == '%', :class => 'group_cb ', :id => "cb_group_all" %> 
        <%= label_tag "cb_group_all", '所有' , :class => 'group_label' %>
        <b class='count'><%= current_enterprise.users.active_or_locked.size %></b>
        <small>......</small>
      </div>
      <div class='clear'></div>
      <% current_enterprise.groups.each do |group| %>
      <div id="group_<%= group.id %>"class="item unchecked" >
        <%= radio_button_tag 'group_id', group.id, @group.try(:id) == group.id, :class => 'group_cb ', :id => "cb_group_#{group.id}" %> 
        <%= label_tag "cb_group_#{group.id}", group.name , :class => 'group_label' %>
        <b class='count'><%= group.users.active_or_locked.size %></b>
        <small>......</small>
      </div>
      <div class='clear'></div>
      <% end %>
      <script type="text/javascript">
        $('.group_cb').click(function() {          
          $('.group_cb:checked').parent().removeClass('unchecked');
          $('.group_cb:not(:checked)').parent().addClass('unchecked');
          $('#users_form #group_id').val($(this).val());
          $('#users_form').submit();
        })
      </script>
    </div>
  </div>  
</div>  